Foreign Keys and Primary Keys


A key in which it is not a primary key in its own table is called a foreign key. Although the key may be a primary key in another table.
A foreign key is a relationship or link between two tables which ensures that the data stored in a database is consistent.
The foreign key link is set up by matching a column in one table to the primary key column in another table.
The primary key is one of the unique keys defined on a table, but is selected to be the key of first importance. There can be only one primary key on a table.
DVD Code
Title
Cost
Date Out
Date Due
002
Fargo
£2.50
03/09/04
04/09/04
003
American Pie
£2.50
01/09/04
02/09/04
008
Reservoir Dogs
£2.50
04/09/04
06/09/04
011
Notting Hill
£2.50
04/09/04
06/09/04
014
Prime Suspect
£2.00
27/08/04
28/08/04

The Primary Key is DVD Code.

Member Number
Member Name
Telephone Number
DVD Code
1012
Isobel Ringer
293847
003
1034
Jonh Silver
142536
008
1056
Fred Flintstone
817263
011

Whereas the Primary Key here is Member Number, and the Foreign Key is the DVD Code.
These two tables are linked by the DVD Code.